﻿using GalaSoft.MvvmLight.Command;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace Calculator2014
{
    public class CalculatorViewModel : INotifyPropertyChanged
    {
        public string Value1 { get; set; }
        public string Value2 { get; set; }
        public string showResult;
        private double number1, number2;

        public string ShowResult 
        { 
            get { return showResult; }
            set 
            {
                ShowResult = value;
                OnPropertyChanged();
            } 
        }
        //TODO : OnPropertyChanged("ShowResult");

        public CalculatorViewModel()
        {
        }

        private RelayCommand _addCommand;
        public RelayCommand AddCommand
        {
            get { return _addCommand ?? (_addCommand = new RelayCommand(AddCommandExecute, CanAddCommandExecute)); }            
        }

        private bool CanAddCommandExecute()
        {
            return true;
        }

        private void AddCommandExecute()
        {            
            if(ValuesAreNumbers())
            {
                var calculatorModel = new CalculatorModel();
                ShowResult = calculatorModel.Add(number1, number2).ToString();
            }
        }

        private void MinCommandExecute()
        {
            if (ValuesAreNumbers())
            {
                var calculatorModel = new CalculatorModel();
                ShowResult = calculatorModel.Sub(number1, number2).ToString();
            }
        }

        private void MulCommandExecute()
        {
            if (ValuesAreNumbers())
            {
                var calculatorModel = new CalculatorModel();
                ShowResult = calculatorModel.Mul(number1, number2).ToString();
            }
        }

        private void DivCommandExecute()
        {
            if (ValuesAreNumbers())
            {
                var calculatorModel = new CalculatorModel();
                ShowResult = calculatorModel.Div(number1, number2).ToString();
            }
        }

        private bool ValuesAreNumbers()
        {
            if (!Double.TryParse(Value1, out number1) || !Double.TryParse(Value2, out number2))
            {
                MessageBox.Show("Wrong values. Only numbers!", "Warning", MessageBoxButton.OK, MessageBoxImage.Exclamation);
                return false;
            }
            else
                return true;                           
        }

        public event PropertyChangedEventHandler PropertyChanged; //Ca serve à quoi exactement?
    }
}
